//
//  AddressPickerView.m
//  iOSTools
//
//  Created by BaizYi on 2018/12/28.
//  Copyright © 2018 YJY. All rights reserved.
//

#import "AddressPickerView.h"

@interface AddressPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property(strong,nonatomic) UIView  *containerView;
@property(strong,nonatomic) UIPickerView  *pickerView;
@property(strong,nonatomic) UIView  *toolBar;
@property(strong,nonatomic) NSArray  *dataArr;
@property(assign,nonatomic) NSInteger firstIndex;
@property(assign,nonatomic) NSInteger secondIndex;
@property(assign,nonatomic) NSInteger thirdIndex;

@end

@implementation AddressPickerView

-(instancetype)initWithFrame:(CGRect)frame resultBlock:(AddressBlock)block{
    if (self = [super initWithFrame:frame]) {
        self.firstIndex = 0;
        self.secondIndex = 0;
        self.thirdIndex = 0;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
        [self configCityData];
        [self addSubview:self.containerView];
        self.block = block;
    }
    return self;
}


-(void)configCityData{
    self.dataArr = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
}


-(void)showInView:(UIView *)view{
    if (view == nil) {
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }else{
        [view addSubview:self];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.containerView.frame = CGRectMake(0, Screen_Height-Screen_Width, Screen_Width, Screen_Width);
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7];
    }];
    
}

-(void)hidden{
    [UIView animateWithDuration:0.5 animations:^{
        self.containerView.frame = CGRectMake(0, Screen_Height, Screen_Width, Screen_Width);
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


-(void)cancelAction{
    [self hidden];
}
-(void)sureAction{
    
    NSString *province = self.dataArr[self.firstIndex][@"state"];
    NSString *city = self.dataArr[self.firstIndex][@"cities"][self.secondIndex][@"city"];
    NSString *result = @"";
    if ([province isEqualToString:@"北京"] || [province isEqualToString:@"重庆"] ||
        [province isEqualToString:@"上海"] || [province isEqualToString:@"天津"] ) {
        result = [NSString stringWithFormat:@"%@市%@区", province, city];
    }else{
        result = [NSString stringWithFormat:@"%@省%@市", province, city];
    }
    
    NSLog(@"--%@", result);
    if (self.block) {
        self.block(result);
    }
    
    [self hidden];
}

#pragma makr - PickerViewDelegate
//列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.dataArr.count;
    }else if (component == 1){
        return [self.dataArr[self.firstIndex][@"cities"] count];
    }else{
        //        return [self.dataArr[self.firstIndex][@"cities"][self.secondIndex][@"areas"] count];
        return @"";
    }
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return self.dataArr[row][@"state"];
    }else if (component == 1){
        NSArray *citiesArr = self.dataArr[self.firstIndex][@"cities"];
        return citiesArr[row][@"city"];
    }else{
        NSArray *citiesArr = self.dataArr[self.firstIndex][@"cities"];
        //        return citiesArr[self.secondIndex][@"areas"][row];
        return @"";
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.firstIndex = row;
        self.secondIndex = 0;
        self.thirdIndex = 0;
        [self.pickerView reloadComponent:1];
        //        [self.pickerView reloadComponent:2];
    }else if (component == 1){
        self.secondIndex = row;
        self.thirdIndex = 0;
        //        [self.pickerView reloadComponent:2];
    }else{
        self.thirdIndex = row;
    }
}




#pragma mark --UI--
-(UIView *)containerView{
    if (_containerView == nil) {
        _containerView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_Height, Screen_Width, Screen_Width)];
        _containerView.backgroundColor = [UIColor whiteColor];
        [_containerView addSubview:self.toolBar];
        [_containerView addSubview:self.pickerView];
    }
    return _containerView;
}
-(UIPickerView *)pickerView{
    if (_pickerView == nil) {
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, Screen_Width, Screen_Width-40)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}
-(UIView *)toolBar{
    if (_toolBar == nil) {
        _toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(10, 2.5, 50, 35);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [_toolBar addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(Screen_Width-60, 2.5, 50, 35);
        [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        [_toolBar addSubview:sureBtn];
        
        UILabel *lineLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 39, Screen_Width, 1)];
        lineLB.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_toolBar addSubview:lineLB];
        
    }
    return _toolBar;
}
-(NSArray *)dataArr{
    if (_dataArr == nil) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}


@end
